import { createRouter, createWebHashHistory } from 'vue-router';


const routes = [
    {
        path: '/',
        children: [
            {
                // path: '/',
                path: '',
                component: () => import('../views/Home.vue'),
                meta: { title: '首页' },
            },
            {
                path: 'teleplay',
                component: () => import('../views/Teleplay.vue'),
                meta: { title: '电视剧' },
            },
            {
                path: 'movie',
                component: () => import('../views/Movie.vue'),
                meta: { title: '电影' },
            },
            {
                path: 'animation',
                component: () => import('../views/Animation.vue'),
                meta: { title: '动漫' },
            },
            {
                path: 'search',
                component: () => import('../views/Search.vue'),
                meta: { title: '搜索结果' },
            },
            {
                path: 'login',
                component: () => import('../views/Login.vue'),
                meta: { title: '登录' },
            },
            {
                path: 'admin/',
                component: () => import('../views/admin/AdminLayout.vue'),
                meta: { requiresAuth: true },
                children: [
                    {
                        path: '',
                        component: () => import('../views/admin/Index.vue'),
                        meta: { title: '后台管理 - 首页' },
                    },
                    {
                        path: 'index',
                        component: () => import('../views/admin/Index.vue'),
                        meta: { title: '后台管理 - 首页' },
                    },
                    {
                        path: 'vmanage',
                        component: () => import('../views/admin/VideoManage.vue'),
                        meta: { title: '后台管理 - 视频管理' },
                    },
                    {
                        path: 'vlmanage',
                        component: () => import('../views/admin/VideoLinkManage.vue'),
                        meta: { title: '后台管理 - 链接管理' },
                    },
                    {
                        path: 'armanage',
                        component: () => import('../views/admin/AnnualReviewManage.vue'),
                        meta: { title: '后台管理 - 年评管理' },
                    },
                    {
                        path: 'collect',
                        component: () => import('../views/admin/VideoCollect.vue'),
                        meta: { title: '后台管理 - 视频采集' },
                    },
                ]
            },
        ],


    },
]


const router = createRouter({
    history: createWebHashHistory(),
    routes, // `routes: routes` 的缩写
});


// 路由守卫（添加标题及检查授权）
router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = to.meta.title ? to.meta.title : '加载中';
    }

    const token = window.sessionStorage.getItem('token');
    // 此路由需要授权，检查是否已登录
    if (to.meta.requiresAuth) {
        if (token) {
            next();
        } else {
            // 如果没有token，则重定向到登录页面
            next({ path: '/login' });
        }
    } else {
        next();
    }
});

export default router;